package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.BeanCopierUtils;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.MessageDao;
import com.qdlc.p2p.dal.dto.Message;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.model.MessageModel;
import com.qdlc.p2p.dal.mybatis.model.mapper.MessageMapper;
import com.qdlc.p2p.dal.mybatis.pager.PageProxyBuilder;
import com.qdlc.p2p.dal.mybatis.pager.core.PageParameter;
import com.qdlc.p2p.dal.util.UserUtils;

@Repository
public class MessageDaoImpl implements MessageDao {

	@Resource
	MessageMapper messageMapper;
	
	/**
	 * 插入记录
	 * @param message
	 */
	@Override
	public Message insert(Message message){
		messageMapper.insert(message);
		return message;
	}
	
	/**
	 * 修改记录
	 * @param message
	 */
	@Override
	public void update(Message message){
		messageMapper.update(message);
	}
	
	@Override
	public int unreadCount(long receiveUser) {
		return messageMapper.unreadCount(receiveUser);
	}

	@Override
	public PageDataList<MessageModel> receiveList(long receiveUserId, int startPage) {
		
		PageProxyBuilder<MessageMapper> builder = PageProxyBuilder.newBuilder(MessageMapper.class);
		List<Message> messageList = builder.setCurrentPage(startPage).createProxy().receiveList(receiveUserId);
		PageParameter pageParameter = builder.getPageParameter();
		
		List<MessageModel> messageModelList = null;
		if(messageList != null){
			messageModelList = new ArrayList<MessageModel>(messageList.size());
			for(Message message: messageList){
				MessageModel model = new MessageModel();
				BeanCopierUtils.copyProperties(message, model);
				User receiveUser = UserUtils.getUserById(model.getReceiveUserId());
				model.setReceiveUserName(receiveUser.getUserName());
				UserCache userCache = UserUtils.getUserCacheByUserId(receiveUserId);
				model.setAvatarPic(userCache.getAvatarPicPath());
				model.setReceiveUserId(0);
				messageModelList.add(model);
			}
		}
		
		PageDataList<MessageModel> messagePageData = new PageDataList<MessageModel>();
		messagePageData.setList(messageModelList);
		messagePageData.setPage(pageParameter.convert2Page());
		
		return messagePageData;
	}

	@Override
	public PageDataList<MessageModel> sentList(long sentUserId, int startPage) {
		
		PageProxyBuilder<MessageMapper> builder = PageProxyBuilder.newBuilder(MessageMapper.class);
		List<Message> messageList = builder.setCurrentPage(startPage).createProxy().sentList(sentUserId);
		PageParameter pageParameter = builder.getPageParameter();
		List<MessageModel> messageModelList = null;
		if(messageList != null){
			messageModelList = new ArrayList<MessageModel>(messageList.size());
			for(Message message: messageList){
				MessageModel model = new MessageModel();
				BeanCopierUtils.copyProperties(message, model);
				User sentUser = UserUtils.getUserById(model.getSentUserId());
				User receiveUser = UserUtils.getUserById(model.getReceiveUserId());
				model.setSentUserName(sentUser.getUserName());
				model.setReceiveUserName(receiveUser.getUserName());
				messageModelList.add(model);
			}
		}
		
		PageDataList<MessageModel> messagePageData = new PageDataList<MessageModel>();
		messagePageData.setList(messageModelList);
		messagePageData.setPage(pageParameter.convert2Page());
		
		return messagePageData;
	}
	
	@Override
	public PageDataList<Message> findByTitle(String title, int pageNumber, int pageSize){
		PageProxyBuilder<MessageMapper> proxyBuilder = PageProxyBuilder.newBuilder(MessageMapper.class);
		List<Message> messageList = proxyBuilder.setPageParameter(pageNumber, pageSize)
												.createProxy()
												.findByTitle(title);
		PageParameter pageParameter = proxyBuilder.getPageParameter();
		PageDataList<Message> pageDataList = new PageDataList<Message>(pageParameter.convert2Page(), messageList);
		return pageDataList;
	}

	@Override
	public Message find(long id, long userId) {
		Message message = messageMapper.find(id, userId);
		return message;
	}

	@Override
	public void setRead(long id, long receiveUserId) {
		messageMapper.setRead(id, receiveUserId);
	}

	@Override
	public void delReceiveList(Object[] ids, long receiveUserId) {
		if(ids == null){
			throw new UserException("请选择删除的接收信息！", 1);
		}
		messageMapper.delReceiveList(ids, receiveUserId);
	}

	@Override
	public void delSentList(Object[] ids, long sentUserId) {
		if(ids == null){
			throw new UserException("请选择删除的发送信息！", 1);
		}
		messageMapper.delSentList(ids, sentUserId);
	}

	@Override
	public void setReadList(Object[] ids, long receiveUserId) {
		if(ids == null){
			throw new UserException("请选择要设置为已读信息的信息！", 1);
		}
		messageMapper.setReadList(ids, receiveUserId);
	}

	@Override
	public void setUnreadList(Object[] ids, long receiveUserId) {
		if(ids == null){
			throw new UserException("请选择要设置为未读信息的信息！", 1);
		}
		messageMapper.setUnreadList(ids, receiveUserId);
	}

}
